package com.crazecoder.flutter.iflyad.utils;

import android.app.Activity;
import android.graphics.Point;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.iflytek.voiceads.IFLYBannerAd;
import com.iflytek.voiceads.config.AdError;
import com.iflytek.voiceads.config.AdKeys;
import com.iflytek.voiceads.listener.IFLYAdListener;

import java.util.HashMap;
import java.util.Map;

public class BannerUtil implements IFLYAdListener {
    private Activity activity;
    private Map<String, IFLYBannerAd> cache;
    private static BannerUtil instance;
    private String adUnitId;

    private BannerUtil(Activity activity) {
        this.activity = activity;
        cache = new HashMap<>();
    }

    public static BannerUtil getInstance(Activity activity) {
        if (instance == null) {
            instance = new BannerUtil(activity);
        }
        return instance;
    }

    public void getBanner(String adUnitId) {
        this.adUnitId = adUnitId;
        //创建横幅广告：adUnitId：开发者在讯飞AI营销云平台(http://www.voiceads.cn/)申请的横幅广告位 ID
        IFLYBannerAd bannerAd = IFLYBannerAd.createBannerAd(activity.getApplicationContext(), adUnitId);
        bannerAd.setParameter(AdKeys.OAID, OAIDUtil.getInstance(activity).getOAID());

        cache.put(adUnitId, bannerAd);
//        return bannerAd;
    }

    public void show(String adUnitId) {
        IFLYBannerAd bannerAd = cache.get(adUnitId);
        if (bannerAd != null) {
            bannerAd.loadAd(this);
            LinearLayout content = new LinearLayout(activity);
            content.setOrientation(LinearLayout.VERTICAL);
            content.setGravity(Gravity.BOTTOM);
            content.addView(bannerAd, getUnifiedBannerLayoutParams());
            activity.addContentView(
                    content,
                    new ViewGroup.LayoutParams(
                            ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        }
    }

    public void dispose(String adUnitId) {
        IFLYBannerAd bannerAd = cache.get(adUnitId);
        if (bannerAd != null) {
            bannerAd.destroy();
            View contentView = (View) bannerAd.getParent();
            if (contentView == null || !(contentView.getParent() instanceof ViewGroup)) return;

            ViewGroup contentParent = (ViewGroup) (contentView.getParent());
            contentParent.removeView(contentView);
        }
        cache.remove(adUnitId);
    }

    private LinearLayout.LayoutParams getUnifiedBannerLayoutParams() {
        Point screenSize = new Point();
        activity.getWindowManager().getDefaultDisplay().getSize(screenSize);
        return new LinearLayout.LayoutParams(screenSize.x, Math.round(screenSize.x / 6.4F));
    }

    /**
     * 广告请求成功
     */
    @Override
    public void onAdReceive() {
        //展示广告
        IFLYBannerAd bannerAd = cache.get(adUnitId);
        if (bannerAd != null)
            bannerAd.showAd();
        Log.d("Ad_Android_Demo", "request success");
    }

    /**
     * 获取广告失败
     */
    @Override
    public void onAdFailed(AdError error) {
        Log.d("Ad_Android_Demo", "request failed:" + error.getErrorCode() + "," + error.getErrorDescription());
    }

    /**
     * 广告被点击
     */
    @Override
    public void onAdClick() {
        Log.d("Ad_Android_Demo", "onAdClick");
    }

    /**
     * 广告被关闭
     */
    @Override
    public void onAdClose() {
        Log.d("Ad_Android_Demo", "onAdClose");
    }

    /**
     * 广告曝光
     */
    @Override
    public void onAdExposure() {
        Log.d("Ad_Android_Demo", "onAdExposure");
    }

    /**
     * 下载确认
     */
    @Override
    public void onConfirm() {
        Log.d("Ad_Android_Demo", "onConfirm");
    }

    /**
     * 下载取消
     */
    @Override
    public void onCancel() {
        Log.d("Ad_Android_Demo", "onCancel");
    }
}
